from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class Base():
    def __init__(self,driver):
        self.driver=driver
    # 封装打开网址
    def open_url(self,url):
        self.driver.get(url)

    # 封装的时间方法
    def get_time(self):
        et=time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_screenshot('../Png'+'Img'+et+'.png')

    # 封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            self.get_time()

    # 封装点击事件
    def click(self,loc):
        self.find_element(loc).click()

    # 封装输入事件
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    # 封装清空事件
    def clear(self,loc):
        self.find_element(loc).clear()

    # 封装文本文本
    def text(self,loc):
       return self.find_element(loc).text

    #多窗口切换
    def new_handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    #frame封装框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

#   退出frame框架
    def frame_uncon(self):
        self.driver.switch_to.default_content()

    # 滚动到任意像素
    def scrollto_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    # 滚动到底部
    def scrollto_height(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

    # 弹窗——accept()
    def accept(self):
        self.driver.switch_to.alert.accept()

    # 弹窗-dismiss
    def dismiss(self):
        self.driver.switch_to.alert.dismiss()

    # 弹窗-send_keys_accept(),点击弹窗交互
    def send_accept(self,info):
        prompt=self.driver.switch_to.alert
        prompt.send_keys(info)
        prompt.accept()

    # 弹窗-send_keys-dismiss()
    def send_dismiss(self,info):
        prompt = self.driver.switch_to.alert
        prompt.send_keys(info)
        prompt.dismiss()

    def select_index(self,loc,x):
        index=Select(self.find_element(loc))
        index.select_by_index(x)

        # 下拉菜单按value值算
    def select_value(self,loc,info):
        value = Select(self.find_element(loc))
        value.select_by_value(info)
        # 下拉菜单按文本值算
    def select_text(self,loc,info):
        vtext=Select(self.find_element(loc))
        vtext.select_by_visible_text(info)

    # 鼠标封装
    # 封装鼠标双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.driver.find_element(loc)).perform()
    # 封装鼠标三击
    def three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()
    # 封装鼠标右击
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()
    # 封装鼠标悬停
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    # 封装鼠标拖拽
    def drag_and_drop(self,loc,x,y):
        ActionChains(self.driver).drag_and_drop_by_offset(self.find_element(loc),x,y).perform()
    # 封装鼠标按住不放
    def click_and_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()

    # 键盘封装
    def keys_enter(self,loc):
        self.send_keys(loc,Keys.ENTER)
    def keys_tab(self,loc):
        self.send_keys(loc, Keys.TAB)
    def keys_up(self,loc):
        self.send_keys(loc,Keys.UP)
    def keys_delete(self,loc):
        self.send_keys(loc,Keys.DELETE)

        self.send_keys(loc,Keys.BACKSPACE)
        self.send_keys(loc,Keys.LEFT)
        self.send_keys(loc,Keys.RIGHT)
        self.send_keys(loc,Keys.HELP)
        self.send_keys(loc,Keys.SHIFT)
        self.send_keys(loc,Keys.ADD)
        self.send_keys(loc, Keys.HOME)
        self.send_keys(loc, Keys.SHIFT)
